﻿/*
--------------------------------------
File: DecisionTreeMake.cs
Project: Themaopdracht 7
Author: Roel Blaauwgeers

Description:
Creates the decision tree. Testset images are still hardcoded! Change these to the prefered images. 
WARNING: use of 'getpixel' in the function 'analyzeTrainingSet' as this is for testing atm. Not ready for implementation.
--------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace THO7_Team8.Filters
{
    class DecisionTreeMake : VisionAlgorithm
    {

            public DecisionTreeMake(String name) : base(name) { }

            public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
            {
                Size bitmapSize;
                List<DataPoint> dataPointList = makeDataPointList();

                Console.WriteLine("Now make tree!");

                //Make a root node
                DecisionTree tree = new DecisionTree(dataPointList);

                //Calculate minimal datapoints
                int minDataPoints = dataPointList.Count / 100 * 2;
                //int minDataPoints = 100000;

                //Make the tree
                tree.partitionRecursively(minDataPoints);

                tree.removeDataPoints();

                Serializer.serialize(tree, "C:\\dtree.dat");

                //Wait for key-press before exit
                //Console.WriteLine("Press any key to continue...");
                //Console.ReadKey(true);

                return sourceImage;
            }

            static List<DataPoint> makeDataPointList()
            {
                //Make a new datapointlist
                List<DataPoint> list = new List<DataPoint>();
                try
                {
                    //Picture 1
                    Console.WriteLine("bmp 1");
                    Bitmap pic1_orig = new Bitmap("C:\\np1.jpg");
                    Bitmap pic1_bw = new Bitmap("C:\\np1_.jpg");

                    HSV[,] hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);


                    //Picture 2
                    Console.WriteLine("bmp 2");
                    pic1_orig = new Bitmap("C:\\np2.jpg");
                    pic1_bw = new Bitmap("C:\\np2_.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);


                    //Picture 3
                    Console.WriteLine("bmp 3");
                    pic1_orig = new Bitmap("C:\\np3.jpg");
                    pic1_bw = new Bitmap("C:\\np3_.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);


                    //Picture 5
                    Console.WriteLine("bmp 5");
                    pic1_orig = new Bitmap("C:\\np5.jpg");
                    pic1_bw = new Bitmap("C:\\np5_.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);

                    //Picture 6
                    Console.WriteLine("bmp 6");
                    pic1_orig = new Bitmap("C:\\t6.jpg");
                    pic1_bw = new Bitmap("C:\\_t6.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);

                    //Picture 7
                    Console.WriteLine("bmp 7");
                    pic1_orig = new Bitmap("C:\\t7.jpg");
                    pic1_bw = new Bitmap("C:\\_t7.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);

                    //Picture 8
                    Console.WriteLine("bmp 8");
                    pic1_orig = new Bitmap("C:\\t8.jpg");
                    pic1_bw = new Bitmap("C:\\_t8.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);

                    //Picture 9
                    Console.WriteLine("bmp 9");
                    pic1_orig = new Bitmap("C:\\t9.jpg");
                    pic1_bw = new Bitmap("C:\\_t9.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);


                    //Picture 10
                    Console.WriteLine("bmp 10");
                    pic1_orig = new Bitmap("C:\\t10.jpg");
                    pic1_bw = new Bitmap("C:\\_t10.jpg");

                    hsvColors = DTUtils.BitmapToArray(pic1_orig);

                    analyzeTrainingSet(list, pic1_orig.Width, pic1_orig.Height, hsvColors, pic1_bw);


                }
                catch (ArgumentException)
                {
                    Console.WriteLine("Cannot find file or file is corrupt. Please check;");
                    //Wait for key-press before exit
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey(true);

                    Environment.Exit(1);
                }
                return list;
            }

            static void analyzeTrainingSet(List<DataPoint> list, int x_border, int y_border, HSV[,] hsvColors, Bitmap bw)
            {
                int patch_dimension = 5;

                //For all pixels
                for (int y = 0; y < y_border; y++)
                {
                    for (int x = 0; x < x_border; x++)
                    {
                        //Get the features of the pixel
                        double[] attributes = DTUtils.getFeatures(x, y, x_border, y_border, hsvColors, patch_dimension);

                        //Check if pixel is foreground (V == 1) or background (V == 0)
                        if (bw.GetPixel(x, y).GetBrightness() == 1)
                        {
                            list.Add(new DataPoint(30, attributes, 1));
                        }
                        else
                        {
                            list.Add(new DataPoint(30, attributes, 0));
                        }
                    }
                }
            }
        }
    }